/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tecken;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import lib.AudioLib;
import lib.Trie;

/**
 *
 * @author Wolf
 */
public class MainModel {

    Date lastAction = new Date();
    String word = "";
    Trie tree = new Trie();
    Properties config;
    Properties combo;
    AudioLib al = new AudioLib();
    boolean crouch = false;

    public MainModel(String fileConfig, String fileCombo) {
        FileInputStream propInFile = null;
        try {
            propInFile = new FileInputStream(fileConfig);
            config = new Properties();
            config.loadFromXML(propInFile);

            propInFile = new FileInputStream(fileCombo);
            combo = new Properties();
            combo.loadFromXML(propInFile);

            Enumeration em = combo.keys();
            while (em.hasMoreElements()) {
                String str = (String) em.nextElement();
                tree.addWord(str);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            try {
                propInFile.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }

    public MainModel() {
    }

    public void readKey(char c) {
        String str = "" + c;

        //Simple Action
        if (tree.getWords(str).size() > 0) {
            List l = tree.getWords(str);
            for (int i = 0; i < l.size(); i++) {
                if (l.get(i).toString().length() == 1) {
                    System.out.println("Play char " + l.get(i).toString());
                    if(c == 's' && crouch == true){
                        al.read(combo.getProperty("neutral"));
                    }else if(c == 's' && crouch == false){
                        al.read(combo.getProperty("s"));
                    }else{
                    al.read(combo.getProperty(l.get(i).toString()));
                    }
                }
            }
        }

        //Combo Action
        if (lastAction.after(new Date(new Date().getTime() - Integer.valueOf(config.getProperty("comboTimeout"))))) {
            word += c;
            if (tree.getWords(word).size() == 1 && tree.getWords(word).get(0).toString().length() > 1) {
                System.out.println("Combo "+word);
                al.read(combo.getProperty(word));
            }
        } else {
            word = "";
        }
        word += c;
        lastAction = new Date();
    }
}
